package com.tt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tt.entity.vo.MovieRankScoreVO;
import com.tt.entity.pojo.MovieRankScorePO;
import com.tt.mapper.MovieRankScorePOMapper;
import com.tt.mapper.MovieRankScoreVOMapper;
import com.tt.service.MovieRankScoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

/**
 * <p>
 * 电影评分排行榜,需要定期更新 服务实现类
 * </p>
 *
 * @author TP
 * @since 2023-03-04 09:14:12
 */
@Service
public class MovieRankScoreServiceImpl extends ServiceImpl<MovieRankScorePOMapper, MovieRankScorePO> implements MovieRankScoreService {

    private final MovieRankScoreVOMapper movieRankScoreVOMapper;

    public MovieRankScoreServiceImpl(MovieRankScoreVOMapper movieRankScoreVOMapper) {
        this.movieRankScoreVOMapper = movieRankScoreVOMapper;
    }


    @Override
    public ArrayList<ArrayList<MovieRankScoreVO>> getRankScore() {
        ArrayList<MovieRankScoreVO> movieTotal;
        ArrayList<MovieRankScoreVO> movieNew;

        // 总榜
            // 分页, 应该在分页处使用排序信息,在page处排序和在wrapper处排序好像是一样的效果
        Page<MovieRankScoreVO> totalPage = new Page<>(1,250); // 第1页， 250条
        OrderItem orderItem = new OrderItem("total_score", false ); // 按total_score逆序
        totalPage.setOrders(Collections.singletonList(orderItem));
        QueryWrapper<MovieRankScoreVO> totalWrapper = new QueryWrapper();
        totalWrapper.select("movie_id","name","release_date","total_score","gmt_modified", "gmt_delete");
        totalWrapper.isNotNull("total_score");

        Page<MovieRankScoreVO> totalPageData = movieRankScoreVOMapper.selectPage(totalPage, totalWrapper);
        movieTotal = new ArrayList<MovieRankScoreVO>(totalPageData.getRecords());

        // 新榜
        Page<MovieRankScoreVO> newPage = new Page<>(1,250); // 第1页， 250条
        QueryWrapper<MovieRankScoreVO> newWrapper = new QueryWrapper();
        // 或者使用"total_score".equals(i.getProperty()) （配合@TableField("total_score")使用）
        newWrapper.select(MovieRankScoreVO.class, i -> !"total_score".equals(i.getColumn()))
                .isNotNull("new_score")
                .orderByDesc("new_score");
        movieNew = new ArrayList<MovieRankScoreVO>(movieRankScoreVOMapper.selectPage(newPage, newWrapper).getRecords());

        ArrayList<ArrayList<MovieRankScoreVO>> arrayLists = new ArrayList<>();
        arrayLists.add(movieTotal);
        arrayLists.add(movieNew);
        return arrayLists;

    }
}


